#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# See how the Bacula Storage Daemon will choose a free Device
# when doing a restore.
#
TestName="virtual-changer-restore-test"
JobName="virtualchangertest"
. scripts/functions

scripts/cleanup
scripts/copy-2disk-drive-confs
##scripts/prepare-disk-changer

CLIENT=2drive2disk

echo "${cwd}/build" >${cwd}/tmp/file-list
#change_jobname Virtual $JobName
start_test

# Turn off Prefer Mounted Volumes so we use 2 drives
outf="${cwd}/tmp/sed_tmp"
echo "s%# Prefer Mounted Volumes%  Prefer Mounted Volumes%g" >${outf}
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
# Comment the next line out to write everything to one drive
#  otherwise, it writes the two jobs to different drives
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "MaximumConcurrentJobs", "10", "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "MaximumConcurrentJobs", "10", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "MaximumConcurrentJobs", "10", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "MaximumConcurrentJobs", "10", "Autochanger")'

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
@#setdebug level=50 storage=Virtual
@#setdebug level=200 client=$CLIENT 
label storage=Virtual volume=TestVolume001 slot=0 Pool=Default drive=0
label storage=Virtual2 volume=TestVolume002 slot=0 Pool=Default drive=0
run job=Virtual level=Full storage=Virtual  Pool=Default yes
run job=Virtual level=Full storage=Virtual2 Pool=Default yes
wait
@#
@# The following jobs should block waiting for a mount
@# This is done on purpose to block the restores from using certain drives 
run job=Virtual level=Full Pool=Inc   storage=Virtual yes
run job=Virtual level=Full Pool=Full  storage=Virtual yes
run job=Virtual level=Full Pool=Inc   storage=Virtual2 yes
run job=Virtual level=Full Pool=Full  storage=Virtual2 yes
@sleep 2
messages
quit
END_OF_DATA

# exit

run_bacula
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
@#setdebug level=150 storage=VirtualRestore
@# restore jobid=1
restore where=${cwd}/tmp/bacula-restores storage=VirtualRestore
3
1
mark *
done
yes
@sleep 5
status dir
wait jobid=7
messages
@# now restore jobid=2
restore where=${cwd}/tmp/bacula-restores storage=VirtualRestore
3
2
mark *
done
yes
@sleep 5
status dir
wait jobid=8
messages
@out /dev/null
@# terminate jobs
cancel all yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=Virtual
stop_bacula

check_two_logs

end_test
